local zhenzhe = fk.CreateSkill {
  name = "tyj_heg__zhenzhe",
}
local H = require "packages.ol_hegemony.util"
local U = require "packages.utility.utility"
Fk:loadTranslationTable {
  ["tyj_heg__zhenzhe"] = "振詟",
  [":tyj_heg__zhenzhe"] = "当你使用【杀】或【决斗】对其他角色造成伤害后，你可以令其攻击范围内的一名角色将手牌数摸至或弃至其的体力值，因此弃牌的角色回复1点体力。",
  ["#tyj__zhenzhe-invoke"] = "是否发动 振詟 令 %dest 攻击范围内的一名角色将手牌数调整至 %arg 张？",
  ["#tyj__zhenzhe-discard"] = "振詟:你需要弃置 %arg 张牌，因此弃置牌后回复1点体力",
  ["@tyj_heg__zhenzhe-draw"] = "摸牌",
  ["@tyj_heg__zhenzhe-dis"] = "弃牌",
  ["@tyj_heg__zhenzhe-recover"] = "回复",
  ["$tyj_heg__zhenzhe1"] = "名震千里，泽被海东。",
  ["$tyj_heg__zhenzhe2"] = "施威除暴，上下咸服。",
}

zhenzhe:addEffect(fk.Damage, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      return data.card and ((data.card.trueName == 'slash') or data.card.name == "duel") and data.to and
      data.to:isAlive()
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = table.filter(room.alive_players, function(p)
      return data.to:inMyAttackRange(p) and #p:getCardIds('h') ~= math.max(data.to.hp, 0)
    end)
    if #tos > 0 then
      local num = math.max(data.to.hp, 0)
      if player:hasShownSkill(zhenzhe.name) then
        for _, p in ipairs(tos) do
          local cnum = #p:getCardIds('h') - num
          if cnum > 0 then
            room:setPlayerMark(p, "@tyj_heg__zhenzhe-dis", cnum)
            room:setPlayerMark(p, "@tyj_heg__zhenzhe-recover", 1)
          else
            room:setPlayerMark(p, "@tyj_heg__zhenzhe-draw", -cnum)
          end
        end
      end
      local to = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        prompt = "#tyj__zhenzhe-invoke::" .. data.to.id .. ":" .. data.to.hp,
        skill_name = zhenzhe.name,
        cancelable = true,
      })
      for _, p in ipairs(tos) do
        room:setPlayerMark(p, "@tyj_heg__zhenzhe-dis", 0)
        room:setPlayerMark(p, "@tyj_heg__zhenzhe-recover", 0)
        room:setPlayerMark(p, "@tyj_heg__zhenzhe-draw", 0)
      end
      if #to > 0 then
        event:setCostData(self, { tos = to })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:doIndicate(player, { to })
    local num = math.max(data.to.hp, 0)
    if to:isAlive() and #to:getCardIds('h') ~= num then
      local n = #to:getCardIds('h') - num
      if n > 0 then
        local cards = room:askToDiscard(to, {
          min_num = n,
          max_num = n,
          include_equip = false,
          skill_name = zhenzhe.name,
          cancelable = false,
          prompt = "#tyj__zhenzhe-discard:::" .. n,
        })
        if #cards > 0 and to:isAlive() then
          room:recover({
            who = to,
            num = 1,
            recoverBy = player,
            skillName = zhenzhe.name
          })
        end
      elseif n < 0 then
        to:drawCards(-n, zhenzhe.name)
      end
    end
  end,
})
return zhenzhe
